import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:provider/provider.dart';
import 'package:weui/weui.dart';
import 'package:yueyin/events/login_event.dart';
import 'package:yueyin/service/login_service.dart';
import 'package:yueyin/utils/yue_bar.dart';

import '../main.dart';

class LoginView extends StatefulWidget {
  const LoginView({Key? key}) : super(key: key);

  @override
  _LoginViewState createState() => _LoginViewState();
}

class _LoginViewState extends State<LoginView> {
  bool _isLogin = false;


  //订阅  ，需要销毁
  late StreamSubscription<LoginEvent> _listen;

  //输入框控制器,需要销毁
  late TextEditingController _username = TextEditingController();

  late TextEditingController _password = TextEditingController();

  late LoginService service;

  late NavigatorState nav = Navigator.of(context);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: bar('登录界面'),
      body: SingleChildScrollView(
        child: Center(
          child: Card(
            margin: EdgeInsets.all(12),
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.all(Radius.circular(4))),
            elevation: 15,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(4),
                  child: Image.asset(
                    'static/images/music-and-identity-n.jpg',
                    width: double.infinity,
                    height: 260,
                    fit: BoxFit.cover,
                  ),
                ),
                SizedBox(height: 16),
                Container(
                  margin: EdgeInsets.all(8),
                  child: Column(
                    children: [
                      TextField(
                        controller: _username,
                        decoration: InputDecoration(
                          labelText: '账号',
                          hintText: '请输入账号',
                          prefixIcon: Icon(Icons.person),
                        ),
                        // autofocus: true,
                        textInputAction: TextInputAction.next,
                      ),
                      TextField(
                        controller: _password,
                        decoration: InputDecoration(
                          labelText: '密码',
                          hintText: '请输入密码',
                          prefixIcon: Icon(Icons.lock),
                        ),
                        obscureText: true,
                        textInputAction: TextInputAction.send,
                        onSubmitted: (v) {
                          print('已提交 $v');
                        },
                      ),
                      SizedBox(height: 16),
                      GestureDetector(
                        child: Container(
                          width: double.infinity,
                          child: Text(
                            '找回密码',
                            style: TextStyle(color: Colors.blue),
                            textAlign: TextAlign.right,
                          ),
                        ),
                      ),
                      SizedBox(height: 16),
                      WeButton(
                        '登录',
                        theme: WeButtonType.primary,
                        loading: _isLogin,
                        onClick: _login,
                      ),
                      SizedBox(height: 16),
                      WeButton(
                        '注册',
                        theme: WeButtonType.primary,
                        onClick: _register,
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  _login() {
    print('登录');
    bus.fire(User(_username.text, _password.text));
    setState(() {
      _isLogin = true;
    });
  }

  _register() {
    print('注册');
    nav.pushNamed('register');
  }

  @override
  void initState() {
    super.initState();
    bus.on().listen((event) => print('User ${event.runtimeType}'));
    print('bus$bus');
    service = context.read<LoginService>();
    _listen = bus.on<LoginEvent>().listen((event) {
      print('receive event');
      if (event.successful) {
        WeToast.success(context)(message: event.message);
        nav.popAndPushNamed('menu');
      } else {
        WeToast.fail(context)(message: event.message);
        setState(() {
          _isLogin = false;
        });
      }
    });
  }

  @override
  void dispose() {
    _password.dispose();
    _username.dispose();
    _listen.cancel();
    super.dispose();
  }
}
